//////////////////////////////////////////////////////////////////////////
// Project:			DTString
// Description:		DTString is a String using the standard C++ coding of the class,
//				in order to be compatible with the MFC code
// Change Time:		2012-2-9 14:13:38
// Code by ccnyou 
//
// Change Time:		2012-2-11 
// Change Log :		for add some method by ccnyou such as operator +(string)
//////////////////////////////////////////////////////////////////////////
#include "StdAfx.h"
#include "DTString.h"

const DTString &DTString::operator=(const char *pStr)
{
	m_string = pStr;
	return *this;
}

const DTString &DTString::operator+=(const char *pStr)
{
	m_string += pStr;
	return *this;
}

DTString operator+(const DTString &str1,const char *pStr)
{
	DTString strRet = str1;
	strRet.m_string += pStr;
	return strRet;
}

const DTString &DTString::operator = (const DTString &Str)
{
    m_string = Str.operator DTPCSTR();
    return *this;
}

const DTString &DTString::operator+=(const DTString &Str)
{
	m_string += Str;
	return *this;
}

DTString operator+(const DTString &Str1,const DTString &Str2)
{
	DTString strRet = Str1;
	strRet.m_string += Str2.m_string;
	return strRet;
}


const DTString &DTString::operator = (const string &Str)
{
	m_string = Str;
	return *this;
}

const DTString &DTString::operator += (const string &Str)
{
	m_string += Str;
	return *this;
}

DTString operator+(const DTString &str1,const string &Str)
{
	DTString strRet = str1;
	strRet.m_string += Str;
	return strRet;
}

DTString::operator DTPCSTR() const
{
	return this->m_string.c_str();
}



ostream &operator <<(ostream &os,DTString &str)
{
	cout<<str.m_string;
	return os;
}

istream &operator >>(istream &is,DTString &str)
{
	cin>>str.m_string;
	return is;
}





DTString::DTString(const char *pStr)
{
	m_string = pStr;
}


DTString::DTString()
{
	m_string = "";
}

DTString::~DTString()
{

}

int DTString::GetLength()
{
	return m_string.length();
}

DTString DTString::Left( int nCount ) const
{
	char *pBuf = (char *)malloc(nCount+1);
	strncpy(pBuf,*this,nCount);
	pBuf[nCount] = '\0';
	DTString strTmp = pBuf;
	free(pBuf);
	return strTmp;
}

DTString DTString::Right( int nCount ) const
{
	DTString strTmp;
	strTmp = m_string.c_str() + m_string.length() - nCount;
	return strTmp;
}

int DTString::Find( char ch ) const
{
	const char *pStr = m_string.c_str();
	const char *pFindRet = strchr(pStr,ch);
	return pFindRet - pStr;
}

int DTString::Find( char ch, int nStart ) const
{
	const char *pStr = m_string.c_str() + nStart;
	const char *pFindRet = strchr(pStr,ch);
	return pFindRet - pStr + nStart;
}

int DTString::Find(char const *pStrToFind,int nStartPos) const
{
	const char *pStr = m_string.c_str() + nStartPos;
	const char *pFindRet = strstr(pStr,pStrToFind);
	return pFindRet - pStr + nStartPos;
}

int DTString::Find(char const *pStrToFind) const
{
	const char *pStr = m_string.c_str();
	const char *pFindRet = strstr(pStr,pStrToFind);
	return pFindRet - pStr;
}





#ifdef _MACOS
#pragma mark -
#pragma mark MACOS

DTString::operator PCNSSTRING() const
{
    const char *pStr = m_string.c_str();
    NSString *str = [NSString stringWithCString:pStr encoding:NSUTF8StringEncoding];
    return str;
}

DTString::DTString(NSString *Str)
{
    LoadNSString(Str);
}

NSString *DTString::ns_str() const
{
    const char *pStr = m_string.c_str();
    NSString *str = [NSString stringWithCString:pStr encoding:NSUTF8StringEncoding];
    return str;
}

DTString &DTString::LoadNSString(NSString *str)
{
    const char *pStr = [str UTF8String];
    m_string = pStr;
    return *this;
}

DTString &DTString::operator = (NSString *str)
{
    const char *pStr = [str UTF8String];
    m_string = pStr;
    return *this;
}

#endif